<?php

class WSB_Fileinfo extends SplFileInfo
{

	/**
	 * Infos ausdrucken (nur zu Debugzwecken).
	 * @return void
	 */
	public function printInfo()
	{
		echo "<table border=\"1\">";
		echo sprintf("<tr><td>Path</td><td>%s</td></tr>", $this->getPath());
		echo sprintf("<tr><td>Filename</td><td>%s</td></tr>", $this->getFilename());
		echo sprintf("<tr><td>Filepath</td><td>%s</td></tr>", $this->getPathname());
		echo sprintf("<tr><td>Realpath</td><td>%s</td></tr>", $this->getRealPath());
		echo sprintf("<tr><td>Typ</td><td>%s</td></tr>", $this->getType());
		echo sprintf("<tr><td>Mime-Typ</td><td>%s</td></tr>", $this->getMimeType());
		echo sprintf("<tr><td>Size</td><td>%s</td></tr>", $this->getSize());
		echo sprintf("<tr><td>Extension</td><td>%s</td></tr>", $this->getExtension());
		echo sprintf("<tr><td>A-Time</td><td>%s</td></tr>", $this->getATime());
		echo sprintf("<tr><td>M-Time</td><td>%s</td></tr>", $this->getMTime());
		echo sprintf("<tr><td>Perms</td><td>%s</td></tr>", decoct($this->getPerms()));
		echo "</table>";
	}

	/**
	 * @return string
	 */
	public function getMimeType()
	{
		return WSB_MimeTypes::getMimeType($this->getPathname());
	}

	/**
	 * Gib den relativen Pfad der aktuellen Datei, ausgehend vom (uebergeordneten) Verzeichnis $dir zurueck.
	 * Das Argument $dir muss ein uebergeordnetes Verzeichnis sein.
	 *
	 * @param WSB_Fileinfo $dir
	 * @return string
	 */
	public function getRelativePathFrom(self $fileinfo)
	{
        $rootPath = $fileinfo->getPathname();
        $rootPath = rtrim($rootPath, '/');
		$relPath = str_replace($rootPath, '' , $this->getPathname());
		return ltrim($relPath, '/');
	}

	/**
	 * Gib den relativen Pfad ausgehend vom ROOT-Verzeichnis zurueck.
	 *
	 * @return string
	 */
	public function getRelativePath()
	{
		return $this->getRelativePathFrom(new self(WSB_PATH));
	}

	/**
	 * Gib den relativen Pfad ausgehend vom Media-Verzeichnis zurueck.
	 *
	 * @return string
	 */
	public function getRelativeMediaPath()
	{
		$config = Zend_Registry::get('Zend_Config');
		return $this->getRelativePathFrom(new self(WSB_PATH . $config->file->filePath));
	}

	/**
	 * Gib den relativen Pfad ausgehend vom Trash-Verzeichnis zurueck.
	 *
	 * @return string
	 */
	public function getRelativeTrashPath()
	{
		$config = Zend_Registry::get('Zend_Config');
		return $this->getRelativePathFrom(new self(WSB_PATH . $config->file->trashPath));
	}

	/**
	 * Gib die Dateiendung zurueck.
	 *
	 * @return string
	 */
	public function getExtension()
	{
		if($this->isFile()) {
			preg_match('/^.*\.([^.]+?)?$/', $this->getFilename(), $matches);
			if(!empty($matches[1])) {
                return strtolower($matches[1]);
			}
		}
		return '';
	}

	/**
	 * Gib das uebergeordnete FileInfo-Objekt zurueck.
	 *
	 * @return WSB_Fileinfo
	 */
	public function getParentFileInfo()
	{
		return new self($this->getPath());
	}

}
